/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.DAO;

import DBConnect.DBConnect;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author cuoi
 */
public class BillDAO implements ICBill{

    @Override
    public ArrayList<Bill> getListBill() {     
        ArrayList<Bill> list = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        if(DBConnect.open()){
            try {
                ps = DBConnect.cnn.prepareStatement("select * from bill");
                rs = ps.executeQuery();
                list = new ArrayList<Bill>();
                int i=0;
                while (rs.next()) {  
                    i++;
                    list.add(new Bill(rs.getString("id_cus"), rs.getString("id_pro"), rs.getInt("quantity"), rs.getInt("price"), rs.getString("time"), rs.getString("state"), rs.getString("ID")));                    
                }
                //JOptionPane.showMessageDialog(null, "so ket qua: "+i);
            } catch (SQLException ex) {
                Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
            }finally{            
                DBConnect.close(ps, rs);
            }                    
        }else{        
            //phai mo ra 1 form cho nhap user nam passs, url , driver
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }                
        return  list;
    }

    @Override
    public Bill addNew(Bill bill) {
      //  throw new UnsupportedOperationException("Not supported yet.");
        PreparedStatement ps = null;
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("INSERT INTO  `bill` (`id_cus` ,`id_pro` ,`quantity` ,`price` ,`time` ,`state` ,`id`) VALUES (?,?,?,?,?,?,?)");
                ps.setString(1, bill.getIdcus());
                ps.setString(2, bill.getIdpro());
                ps.setInt(3, bill.getQuan());
                ps.setInt(4, bill.getPrice());
                ps.setString(5, bill.getTime());
                ps.setString(6, bill.getState());
                ps.setString(7, bill.getId());                
                if (ps.executeUpdate() > 0) {
                    System.out.print("ok");  //ok
                } else {
                    bill = null;
                }      
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi ");
          } finally {
                 DBConnect.close(ps);
          }        
      } else {
            bill = null;
      }
        return bill;
    }


    public boolean update(String id) {
        throw new UnsupportedOperationException("Not supported yet.");               
    }


    public boolean delete(String id) {
       // throw new UnsupportedOperationException("Not supported yet.");
        PreparedStatement ps = null; 
        if (DBConnect.open()) {
            try {
                ps = DBConnect.cnn.prepareStatement("delete FROM `bill` WHERE `ID` = ?");
                ps.setString(1,id);
                int rs = ps.executeUpdate();
       
                if(rs>0) {
                    return true;
                }
          } catch (SQLException ex) {
              JOptionPane.showMessageDialog(null, "Loi ket noi ");
          } finally {
                 DBConnect.close(ps);
          }        
        }else{        
            //phai mo ra 1 form cho nhap user nam passs, url , driver
            JOptionPane.showMessageDialog(null, "Sai cau hinh ket noi den server");
        }                
        return false;
    }




}
    
